﻿using System;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.IO;

namespace WXYW
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string[] productData = File.ReadAllLines(@"D:\\Product.txt");
            //for (int index = 0; index < productData.Length; index++)
            //{
            //    if (!string.IsNullOrEmpty(productData[index]))
            //    {
            //        ProductClassService service = new ProductClassService();
            //        ProductType productType = new ProductType();
            //        productType.ProductTypeName = productData[index];
            //        productType.ProductClass = service.GetProductClass(7);
            //        service.AddProductType(productType);
            //    }
            //}            
        }

        protected void ChangeTechLogin_Authenticate(object sender, AuthenticateEventArgs e)
        {
            UserService userService = new UserService();
            User user = userService.Logon(WXYWLogin.UserName, WXYWLogin.Password);
            if (user != null)
            {
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                        1,
                        WXYWLogin.UserName,
                        DateTime.Now, DateTime.Now.AddMinutes(2880),
                        WXYWLogin.RememberMeSet,
                        user.UserName);
                string encyptedTicket = FormsAuthentication.Encrypt(authTicket);
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encyptedTicket);
                Response.Cookies.Add(authCookie);

                e.Authenticated = true;
                if (!user.UserTypeReference.IsLoaded)
                {
                    user.UserTypeReference.Load();
                }
                if (!user.ServiceStationReference.IsLoaded)
                {
                    user.ServiceStationReference.Load();
                }
                ContextService.CurrentAccount = user;
                LogUtility.LogUtilityIntance.LogMessgae(string.Format("用户登录:{0}, {1}", user.UserName, user.UserEmail));
                Response.Redirect("Index.aspx");
            }
            else
            {

            }
        }

        protected void RegisterButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserRegister.aspx");
        }
    }
}
